package org.xhnews.preferences;

import java.io.IOException;

import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

public class PreferencesModel {
	private static RecordStore rs;

	public static final String NAME = "ConfDB";

	public static void open() {
		try {
			rs = RecordStore.openRecordStore(NAME, true);
		} catch (RecordStoreException e) {
			e.printStackTrace();
		}
	}

	public static void close() {
		try {
			rs.closeRecordStore();
		} catch (RecordStoreException ex) {
			ex.printStackTrace();
		}
	}

	public static void save() {
		try {
			byte[] record = PreferencesEntry.serialize();
			if (rs.getNumRecords() == 0) {
				rs.addRecord(record, 0, record.length);

			} else {
				rs.setRecord(1, record, 0, record.length);
			}
		} catch (RecordStoreException ex) {
			ex.printStackTrace();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	public static void load() {
		try {
			PreferencesEntry.deserialize(rs.getRecord(1));
		} catch (IOException ex) {
			ex.printStackTrace();
		} catch (RecordStoreException ex) {
			ex.printStackTrace();
		}
	}
}
